$(function () {
    var goodsId = window.location.search.substr(1).split("=")[1];
    $.ajax({
        type: "get",
        url: config.baseUrl + "goods/getGoods.json",
        data: {"goodsId": goodsId},
        dataType: "json",
        success: function (data) {
            if(data!=null){
                var tempData = template('goods-detail-template',data);
                $("#goods-detail-content").html(tempData);
            }
        }
    })

    /*
    * 保存购物车
    * */
    $(document).on('click','#saveCart',function () {
        var param = {
            userId: config.getUserId(),
            goodId:$("#id").val()
        }
        $.ajax({
            type: "post",
            url: config.baseUrl + "carts/addCart.json",
            data: param,
            dataType: "json",
            success: function (data) {
                if(data.status == '200'){
                    alert("添加成功")
                }
            }
        })
    })
})

//更新购物车数量
function updateCartNum(cartId,type){
    var num = $("#num-value").val();
    if(type=='add'){
        num = parseInt(num)+1;
        $("#num-value").val(num);
    }
    if(type=='sub'){
        if(num<=1){
            return false;
        }
        num = parseInt(num)-1;
        $("#num-value").val(num);
    }

    $.ajax({
        type: "post",
        url: config.baseUrl + "carts/updateCart.json",
        data: {
            cartId:cartId,
            num:$("#num-value").val()
        },
        dataType: "json",
        success: function (data) {
            /*if(data.status == '200'){
                alert(data.msg)
            }*/
        }
    })
}